package com.hqyj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Student;
import com.hqyj.pojo.Student;
import com.hqyj.dao.StudentMapper;
import com.hqyj.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-11-15
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public HashMap<String, Object> select(Student student) {
        HashMap<String, Object> map = new HashMap<>();
        // 分页对象
        Page<Student> studentPage = new Page<Student>(student.getPage(),student.getRow());

        // 条件构造器
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        // 获取分页结果集
        Page<Student> page = studentMapper.selectPage(studentPage, studentQueryWrapper);

        // 获取返回的分页结果集对象
        map.put("info","success");
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());

        return map;
    }

    @Override
    public HashMap<String, Object> add(Student student) {
        HashMap<String ,Object> map=new HashMap<>();
        //新增
        int num=studentMapper.insert(student);
        if (num>0){
            map.put("info","保存成功");

        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Student student) {
        HashMap<String ,Object> map=new HashMap<>();
        //修改
        int num=studentMapper.updateById(student);
        if (num>0){
            map.put("info","保存成功");

        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer studentId) {
        HashMap<String ,Object> map=new HashMap<>();
        //删除
        int num=studentMapper.deleteById(studentId);
        if (num>0){
            map.put("info","保存成功");

        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public void excelStudent(HttpServletResponse response) {
        OutputStream output = null;

        try {

            //创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建HSSFSheet对象
            HSSFSheet sheet = wb.createSheet();

            //创建HSSFRow对象，先写入列
            HSSFRow rowName = sheet.createRow(0);
            //写入 第一列的列明
            rowName.createCell(0).setCellValue("编号");
            rowName.createCell(1).setCellValue("姓名");
            rowName.createCell(2).setCellValue("性别");
            rowName.createCell(3).setCellValue("考试号");
            rowName.createCell(4).setCellValue("密码");



            //查询所有用户信息
            List<Student> studentList = studentMapper.selectList(null);

            //从第二行开始写入数据
            for (int i = 1; i <= studentList.size(); i++) {

                HSSFRow row = sheet.createRow(i);
                //编号
                row.createCell(0).setCellValue(studentList.get(i - 1).getStudentId());
                //姓名
                row.createCell(1).setCellValue(studentList.get(i - 1).getStudentName());
                //性别
                row.createCell(2).setCellValue(studentList.get(i - 1).getStudentSex());
                //考试号
                row.createCell(4).setCellValue(studentList.get(i - 1).getStudentNumber());
                //密码
                row.createCell(5).setCellValue(studentList.get(i - 1).getStudentPwd());


            }

            //输出Excel文件到页面
            output = response.getOutputStream();
            String fileName = "客户表";
            //解决中文名下载，编程下滑问题
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1") + "";
            response.reset();
            ;
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            response.setContentType("application/msexcel");
            wb.write(output);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
